package gspushmq.v1.websocket.core;

import java.net.InetSocketAddress;

import org.java_websocket.WebSocket;
import org.java_websocket.server.WebSocketServer;

import gspushmq.model.Message;
import gspushmq.model.User;
import gspushmq.util.ClearMemoryUtil;
import gspushmq.v1.save.log.LogBean;
import gspushmq.v1.save.log.LogQueue;
import gspushmq.v1.subscribe.GGSubscribe;

public abstract class GGWebSocket extends WebSocketServer {


	
	public GGWebSocket(InetSocketAddress address) {
		super(address);
	}

	/**
	 * 写订阅日志
	 * @param
	 * @param status
	 * @author yutao
	 * @date 2018年11月21日下午3:33:03
	 */
	public void writeLog(WebSocket wst, String status) {
		writeLog(wst, status, null);
	}
	
	public void writeLog(WebSocket wst, String status, Message mess) {
		if(wst == null) {
			return;
		}
		User user = GGSubscribe.wstUser.get(wst);
		if(user == null) {
			return;
		}
		String accountId = user.getAccountId();
		if (mess == null) {
			mess = new Message();
			mess.setAccount_id(accountId);
			mess.setWst(wst);
			mess.setProduct_line(user.getProductLine());
		}
		LogBean log = new LogBean(mess);
		log.setStatus(status);
		
		LogQueue.queue.add(log);
	}
	
	/**
	 * 清理api内存数据
	 * @param wst
	 * @author yutao
	 * @date 2018年11月22日上午9:49:52
	 */
	public void cleanMemory(WebSocket wst) {
		ClearMemoryUtil.clearMemory(wst);
	}
}
